package com.ptszyxx.popose.module.base.register.vm;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.ActionEnum;
import com.ptszyxx.popose.common.enums.BannerEventEnum;
import com.ptszyxx.popose.common.utils.YActionEventUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YLoginUtil;
import com.ptszyxx.popose.common.utils.YMonitorUtil;
import com.ptszyxx.popose.common.utils.YWhiteIpUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.login.LoginResult;
import com.ysg.utils.YMD5Utils;
import com.ysg.utils.YPhoneUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.utils.constant.BaseConstants;

import java.util.HashMap;
import java.util.Map;

public class RegisterVM extends BaseViewModel<CommonRepository> {
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> code = new ObservableField<>();
    public ObservableField<String> password = new ObservableField<>();
    public ObservableField<String> inviteCode = new ObservableField<>();
    public ObservableField<Boolean> privacyChecked = new ObservableField<>(false);

    public RegisterVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    @Override
    public void requestUploadAction() {
        YActionEventUtil.getInstance().upload(this, model, ActionEnum.register);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onSendCodeEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onSendCodeCommand = new BindingCommand(()->sendCode());
    public BindingCommand onRegisterCommand = new BindingCommand(()->requestRegister());
    public BindingCommand onAgreementCommand = new BindingCommand(()->YActivityUtil.getInstance().jumpAgreement(this));
    public BindingCommand onPrivacyCommand = new BindingCommand(()->YActivityUtil.getInstance().jumpPrivacy(this));


    private void sendCode() {
        if (YStringUtil.isEmpty(phone.get())){
            YToastUtil.showShort(R.string.et_phone_hint);
            return;
        }
        requestCode();
    }

    public void requestCode(){
        Map<String, String> map = new HashMap<>();
//        map.put("areaCode", );
        map.put("loginname", phone.get());
        map.put("packageName", BaseConstants.PACKAGE_NAME);

        HttpUtils.getInstance()
                .data(model.sendCode(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YToastUtil.showShort(R.string.send_success);
                                uc.onSendCodeEvent.call();
                            }
                        });
    }


    private void requestRegister(){
        if (!checkRegister())return;

        Map<String, String> map = new HashMap<>();
        map.put("loginname", phone.get());
        map.put("code", code.get());
        map.put("password", YMD5Utils.get(password.get()));
        map.put("mobileSpec", YPhoneUtil.getModel());
        map.put("loginType", "2");
        if (YStringUtil.isNotEmpty(inviteCode.get())){
            map.put("invitationcode", inviteCode.get());
        }
        map.put("packageName", BaseConstants.PACKAGE_NAME);
        map.put("oaid", YSPUtils.getInstance().getOAID());

        HttpUtils.getInstance()
                .data(model.login(map), this,
                        new OnSuccessResult<LoginResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<LoginResult> response) {
                                YToastUtil.showShort(R.string.register_success);
                                YMonitorUtil.getInstance().submit(BannerEventEnum.register);
                                requestIsWhiteIp();
                                YLoginUtil.getInstance().login(RegisterVM.this, response.getData());
                            }
                        });
    }

    /**
     * 白名单ip
     */
    public void requestIsWhiteIp() {
        YWhiteIpUtil.getInstance().requestIsWhiteIp(this, model);
    }


    private boolean checkRegister(){
        if (YStringUtil.isEmpty(phone.get())){
            YToastUtil.showShort(R.string.et_phone_hint);
            return false;
        }
        if (YStringUtil.isEmpty(password.get())){
            YToastUtil.showShort(R.string.et_password_hint);
            return false;
        }
        if (!privacyChecked.get()){
            YToastUtil.showShort(R.string.login_privacy_check_tip);
            return false;
        }
        return true;
    }
}
